package io.cloud.tec.project.service;

import io.cloud.tec.project.component.RestApi;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class JdkService {

    public List<String> getDownloadLink(String version) {
        String html = RestApi.jdkDownloadLinkPageHtml();
        Document htmlDocument = Jsoup.parse(html);
        Elements downLinks = htmlDocument.getElementsByClass("license-link icn-download");
        List<String> downLinkList = new ArrayList<>(downLinks.size());
        downLinks.forEach(downLink -> {
            downLinkList.add("https:" + downLink.attr("data-file"));
        });
        return downLinkList.stream().filter(l -> l.contains(version)).collect(Collectors.toList());
    }
}
